package com.atues.service.impl;

import com.atues.domain.ResponseResult;
import com.atues.domain.SystemConstants;
import com.atues.emnu.AppHttpCodeEnum;
import com.atues.service.UploadService;
import com.atues.utils.ImageTypeChecker;
import com.atues.utils.OssUtil;
import com.atues.utils.PathUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class UploadServiceImpl implements UploadService {
    @Override
    public ResponseResult uploadImg(MultipartFile imgFile) {
        if(imgFile == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.CONTENTNOTNULL);
        if(!ImageTypeChecker.isImage(imgFile.getOriginalFilename()))
            return ResponseResult.errorResult(AppHttpCodeEnum.IMAGE_FILE_TYPE_ERROR);
        String selfFileName = PathUtils.generateFilePath(imgFile.getOriginalFilename());
        //自定义文件名
        String fileName = OssUtil.uploadFileToOss(imgFile, SystemConstants.BOLG_AVATAR_ROOT_PATH, selfFileName);
        return ResponseResult.okResult(SystemConstants.ALIYUN_OSS_PREFIX+fileName);
    }
}
